package com.macro.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.macro.mall.dao.OmsOrderDao;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.HomeStatisticalService;
import com.macro.mall.service.SmsHomeAdvertiseService;
import com.macro.mall.utils.DateUtils;
import com.macro.mall.vo.OrderTotalVO;
import com.macro.mall.vo.StatisticalVO;
import com.macro.mall.vo.TodayOrderTotalVO;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomeStatisticalServiceImpl implements HomeStatisticalService {
    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OmsOrderDao omsOrderDao;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;
    @Autowired
    private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;
    @Override
    public StatisticalVO list() {
        SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd");
        StatisticalVO statisticalVO = new StatisticalVO();
        //今日订单总数
        statisticalVO.setTodayOrderTotal(omsOrderDao.todayOrderTotal(t.format(new Date())));
        //今日营销金额
        List<OmsOrder> omsOrders = omsOrderDao.todaySales(t.format(new Date()));
        BigDecimal pay = new BigDecimal("0.00");
        for (OmsOrder o: omsOrders) {
            pay=pay.add(o.getPayAmount());
        }
        statisticalVO.setTodaySalesTotal(pay);
        //昨日营销金额
        List<OmsOrder> omsOrders1 = omsOrderDao.yesterdaySales(t.format(new Date()));
        BigDecimal pay1 = new BigDecimal("0.00");
        for (OmsOrder o: omsOrders1) {
            pay1=pay1.add(o.getPayAmount());
        }
        statisticalVO.setYesterdaySalesTotal(pay1);
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andDeleteStatusEqualTo(0);
        List<OmsOrder> omsOrders2 = omsOrderMapper.selectByExample(example);
        for (OmsOrder omsOrder: omsOrders2) {
            if (omsOrder.getStatus()==0) {
                //待付款
                statisticalVO.setObligationOrderTotal(statisticalVO.getObligationOrderTotal()+1);
            }
            if (omsOrder.getStatus()==1) {
                //待发货订单总数
                statisticalVO.setWaitSendOutOrderTotal(statisticalVO.getWaitConfirmReceivingOrderTotal() + 1);
            }
            if (omsOrder.getStatus()==2){
                //已发货订单总数
                statisticalVO.setShippedOrderTotal(statisticalVO.getShippedOrderTotal()+1);
            }
            if (omsOrder.getStatus()==3){
                //已收货
                statisticalVO.setCompletedOrderTotal(statisticalVO.getCompletedOrderTotal()+1);
            }
                //带确认收货
            if (omsOrder.getStatus()==3 && omsOrder.getConfirmStatus()==0){
                statisticalVO.setWaitConfirmReceivingOrderTotal(statisticalVO.getWaitConfirmReceivingOrderTotal()+1);
            }


        }
        PmsProductExample pmsProductExample = new PmsProductExample();
        pmsProductExample.createCriteria().andDeleteStatusEqualTo(0);
        List<PmsProduct> pmsProducts= pmsProductMapper.selectByExample(pmsProductExample);
        for (PmsProduct pmsProduct: pmsProducts) {
            //缺货登记
            if (pmsProduct.getStock()==0){
                statisticalVO.setStockoutRegisterOrderTotal(statisticalVO.getStockoutRegisterOrderTotal()+1);}
            if (pmsProduct.getPublishStatus()==0){
                //商品上架数
                statisticalVO.setProductOutOfStockTotal(statisticalVO.getProductOutOfStockTotal()+1);}
            if (pmsProduct.getPublishStatus()==1){
                //商品下架数
                statisticalVO.setProductAddedTotal(statisticalVO.getProductAddedTotal()+1);}
            if (pmsProduct.getStock()<=pmsProduct.getLowStock()){
                statisticalVO.setInventoryNervousTotal(statisticalVO.getInventoryNervousTotal()+1);
            }
        }
            //商品总数
        statisticalVO.setProductTotal(pmsProducts.size());
        List<UmsMember> umsMembers = umsMemberMapper.selectByExample(null);
        for (UmsMember umsMember: umsMembers) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
            if (sdf.format(umsMember.getCreateTime()).equals(sdf.format(new Date()))){
                //今天用户新增数量
                statisticalVO.setTodayAddUserTotal(statisticalVO.getTodayAddUserTotal()+1);
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(umsMember.getCreateTime());
            calendar.add(Calendar.DATE, -1);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(new Date());
            calendar1.add(Calendar.DATE, -1);
            if (sdf.format(calendar.getTime()).equals(sdf.format(calendar1.getTime()))){
                //昨天用户新增数量
                statisticalVO.setYesterdayAddUserTotal(statisticalVO.getYesterdayAddUserTotal()+1);
            }
            if (sdf1.format(umsMember.getCreateTime()).equals(sdf1.format(new Date()))){
                statisticalVO.setCurAddUserTotal(statisticalVO.getCurAddUserTotal()+1);
            }
        }
        statisticalVO.setUserTotal(umsMembers.size());
        //广告
        SmsHomeAdvertiseExample smsHomeAdvertiseExample = new SmsHomeAdvertiseExample();
        smsHomeAdvertiseExample.createCriteria().andSortEqualTo(0);
        List<SmsHomeAdvertise> smsHomeAdvertises = smsHomeAdvertiseMapper.selectByExample(smsHomeAdvertiseExample);
        for (SmsHomeAdvertise smsHomeAdvertise: smsHomeAdvertises) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
            if ((Integer.parseInt(sdf.format(new Date()))-Integer.parseInt(sdf.format(smsHomeAdvertise.getEndTime())))<=1 && (Integer.parseInt(sdf.format(new Date()))-Integer.parseInt(sdf.format(smsHomeAdvertise.getEndTime())))!=11){
                statisticalVO.setAdvertisingBitDueBeAboutToDoExpireTotal(statisticalVO.getAdvertisingBitDueBeAboutToDoExpireTotal()+1);
            }
        }
        //待处理退款申请总数
        OmsOrderReturnApplyExample omsOrderReturnApplyExample = new OmsOrderReturnApplyExample();
        omsOrderReturnApplyExample.createCriteria().andStatusEqualTo(0);
        omsOrderReturnApplyMapper.selectByExample(omsOrderReturnApplyExample);
        return statisticalVO;
    }

    @Override
    public OrderTotalVO monthOrderStatistical() {
//        int lastMonthOrderTotal=0;
//        int laseWeekOrderTotal=0;
//        double lastMontPay =0;
//        double laseWeekPay =0;
        OmsOrderExample example = new OmsOrderExample();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String time =sdf.format(new Date());
        example.createCriteria().andDeleteStatusEqualTo(0).andPaymentTimeIsNotNull().andStatusNotEqualTo(4).andCreateTimeLike("%"+time+"%");
        List<OmsOrder> omsOrders = omsOrderMapper.selectByExample(example);
        OrderTotalVO orderTotalVO = new OrderTotalVO();
        orderTotalVO.setMonthOrderTotal(omsOrders.size());
        BigDecimal pay = new BigDecimal("0.00");
        if (CollUtil.isNotEmpty(omsOrders)){
            for (OmsOrder o:omsOrders){
                pay=pay.add(o.getPayAmount());
            }
        }
        Map<String, String> map = DateUtils.getTimeInterval(new Date());
        OmsOrderExample example1 = new OmsOrderExample();
        String start =map.get("start");
        String and = map.get("start");
        example1.createCriteria().andDeleteStatusEqualTo(0).andStatusNotEqualTo(4).andStatusNotEqualTo(5).andPaymentTimeBetween(start, and);
        List<OmsOrder> omsOrders1 = omsOrderMapper.selectByExample(example1);
        orderTotalVO.setWeekOrderTotal(omsOrders1.size());
        BigDecimal pay1 = new BigDecimal("0.00");
        if (CollUtil.isNotEmpty(omsOrders1)){
            for (OmsOrder o:omsOrders1){
                pay1=pay1.add(o.getPayAmount());
            }
        }
        orderTotalVO.setMonthSalesrAmountTotal(pay);
        orderTotalVO.setWeekSalesrAmountTotal(pay1);
        //上月订单数
//        Calendar c = Calendar.getInstance();
//        c.setTime(new Date());
//        c.add(Calendar.MONTH, -1);
//        Date m = c.getTime();
//        OmsOrderExample example2 = new OmsOrderExample();
//        example2.createCriteria().andDeleteStatusEqualTo(0)
//                .andPaymentTimeIsNotNull()
//                .andCreateTimeLike("%"+sdf.format(m)+"%");
//        List<OmsOrder> omsOrders2 = omsOrderMapper.selectByExample(example2);
//        lastMonthOrderTotal=omsOrders2.size();
//        for (OmsOrder o:omsOrders2){
//            lastMontPay=lastMontPay+o.getPayAmount().doubleValue();
//        }
//        Map<String, String> map1 = DateUtils.getLastTimeInterval();
//        OmsOrderExample example3 = new OmsOrderExample();
//        example1.createCriteria()
//                .andDeleteStatusEqualTo(0)
//                .andStatusNotEqualTo(4)
//                .andStatusNotEqualTo(5)
//                .andPaymentTimeBetween(map1.get("start"), map1.get("and"));
//        List<OmsOrder> omsOrders3 = omsOrderMapper.selectByExample(example3);
//        orderTotalVO.setWeekOrderTotal(omsOrders1.size());
//        for (OmsOrder o:omsOrders3){
//            laseWeekPay=laseWeekPay+o.getPayAmount().doubleValue();
//        }
//        System.out.println(lastMonthOrderTotal);
//        System.out.println(laseWeekOrderTotal);
//
//        int  x=0;
//        x=orderTotalVO.getMonthOrderTotal()-lastMonthOrderTotal;
//        lastMonthOrderTotal=x/lastMonthOrderTotal;
//        lastMonthOrderTotal=lastMonthOrderTotal*100;
//        x=0;
//        System.out.println(x);
//        x=orderTotalVO.getWeekOrderTotal()-laseWeekOrderTotal;
//        laseWeekOrderTotal=x/laseWeekOrderTotal;
//        laseWeekOrderTotal=laseWeekOrderTotal*100;
//        orderTotalVO.setMenthOrderProportion(lastMonthOrderTotal+"%");
//        orderTotalVO.setWeekOrderProportion(laseWeekOrderTotal+"%");
//        double n=0;
//        n=orderTotalVO.getMonthSalesrAmountTotal().doubleValue()-lastMontPay;
//        lastMontPay=n/lastMontPay;
//        lastMontPay=lastMontPay*100;
//        n=0;
//        n=orderTotalVO.getWeekSalesrAmountTotal().doubleValue()-laseWeekPay;
//        laseWeekPay=n/laseWeekPay;
//        laseWeekPay=laseWeekPay*100;
//        orderTotalVO.setWeekOrderProportion(laseWeekPay+"%");
//        orderTotalVO.setMenthOrderProportion(lastMontPay+"%");
        return orderTotalVO;
    }

    @Override
    public List<TodayOrderTotalVO> timeOrderTotal(String start, String end) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<Date> dates = DateUtils.findDates(sdf.parse(start), sdf.parse(end));
            List<TodayOrderTotalVO> totalVOS = new ArrayList<>();
            for (Date d:dates){
                TodayOrderTotalVO todayOrderTotalVO = new TodayOrderTotalVO();
                todayOrderTotalVO.setOrderCount(omsOrderDao.todayOrderTotal(sdf.format(d)));
                BigDecimal pay = new BigDecimal("0.00");
                List<OmsOrder> omsOrders = omsOrderDao.todaySales(sdf.format(d));
                if (!omsOrders.isEmpty()){
                    for (OmsOrder o: omsOrders) {
                        pay=pay.add(o.getPayAmount());
                    }
                }
                todayOrderTotalVO.setOrderAmount(pay);
                todayOrderTotalVO.setDate(sdf.format(d));
                totalVOS.add(todayOrderTotalVO);
            }
            return totalVOS;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
